const express = require('express')
const db = require('./db') // 首先引入express模块，不了解去看nodejs教程 安装：npm install express

const app = express()
const cors = require('cors') // 这个比较重要，解决跨域问题.npm install cors 装一下
app.use(express.json())
app.use(express.urlencoded({ extended: true }))
app.use(cors())
app.use(express.json())
app.use(express.urlencoded({ extended: true }))
// 全部数据
app.get('/user', function (req, res) {
  db('select * from dongchedi_ershouche_2', (err, result) => {
    if (err) throw err
    res.send({ status: 1, message: '获取车源信息成功', data: result })
  })
})
app.get('/dongchedi1', function (req, res) {
  db('select * from dongchedi_ershouche_3', (err, result) => {
    if (err) throw err
    res.send({ status: 1, message: '获取车源信息成功', data: result })
  })
})
app.get('/guazi', function (req, res) {
  db('select * from guazi_ershouche_2', (err, result) => {
    if (err) throw err
    res.send({ status: 0, message: '获取车源信息成功', data: result })
  })
})
app.get('/guazi1', function (req, res) {
  db('select * from guazi_ershouche_3', (err, result) => {
    if (err) throw err
    res.send({ status: 0, message: '获取车源信息成功', data: result })
  })
})
app.get('/renrenche', function (req, res) {
  db('select * from renrenche_ershouche_2', (err, result) => {
    if (err) throw err
    res.send({ status: 0, message: '获取车源信息成功', data: result })
  })
})
app.get('/renrenche1', function (req, res) {
  db('select * from renrenche_ershouche_3', (err, result) => {
    if (err) throw err
    res.send({ status: 0, message: '获取车源信息成功', data: result })
  })
})
app.get('/qichejie', function (req, res) {
  db('select * from qichejie_ershouche_2', (err, result) => {
    if (err) throw err
    res.send({ status: 0, message: '获取车源信息成功', data: result })
  })
})
app.get('/qichejie1', function (req, res) {
  db('select * from qichejie_ershouche_3', (err, result) => {
    if (err) throw err
    res.send({ status: 0, message: '获取车源信息成功', data: result })
  })
})
// 获取商家
app.get('/qichejie1', function (req, res) {
  db('select * from qichejie_ershouche_2', (err, result) => {
    if (err) throw err
    res.send({ status: 0, message: '获取车源信息成功', data: result })
  })
})
// 根据日期获取懂车帝今日总数据-2
app.post('/dongchetodayall', function (req, res) {
  const { dt } = req.body
  db(`select * from dongchedi_ershouche_2 where SUBSTRING(dt,1,10)="${dt}"`, (err, result) => {
    if (err) throw err
    res.send({ status: 0, message: '获取车源信息成功', data: result })
  })
})
// 根据日期获取懂车帝今日新增数据-3
app.post('/dongchetodayadd', function (req, res) {
  const { dt } = req.body
  db(`select * from dongchedi_ershouche_3 where SUBSTRING(dt,1,10)="${dt}"`, (err, result) => {
    if (err) throw err
    res.send({ status: 0, message: '获取车源信息成功', data: result })
  })
})
// 根据日期获取瓜子今日总数据-2
app.post('/guazitodayall', function (req, res) {
  const { dt } = req.body
  db(`select * from guazi_ershouche_2 where SUBSTRING(dt,1,10)="${dt}"`, (err, result) => {
    if (err) throw err
    res.send({ status: 0, message: '获取车源信息成功', data: result })
  })
})
// 根据日期获取瓜子今日新增数据-3
app.post('/guazitodayadd', function (req, res) {
  const { dt } = req.body
  db(`select * from guazi_ershouche_3 where SUBSTRING(dt,1,10)="${dt}"`, (err, result) => {
    if (err) throw err
    res.send({ status: 0, message: '获取车源信息成功', data: result })
  })
})
// 根据日期获取汽车街今日总数据-2
app.post('/qichetodayall', function (req, res) {
  const { dt } = req.body
  db(`select * from qichejie_ershouche_2 where SUBSTRING(dt,1,10)="${dt}"`, (err, result) => {
    if (err) throw err
    res.send({ status: 0, message: '获取车源信息成功', data: result })
  })
})
// 根据日期获取汽车街今日新增数据-3
app.post('/qichetodayadd', function (req, res) {
  const { dt } = req.body
  db(`select * from qichejie_ershouche_3 where SUBSTRING(dt,1,10)="${dt}"`, (err, result) => {
    if (err) throw err
    res.send({ status: 0, message: '获取车源信息成功', data: result })
  })
})
// 根据日期获取人人车今日总数据-2
app.post('/renrentodayall', function (req, res) {
  const { dt } = req.body
  db(`select * from renrenche_ershouche_2 where SUBSTRING(dt,1,10)="${dt}"`, (err, result) => {
    if (err) throw err
    res.send({ status: 0, message: '获取车源信息成功', data: result })
  })
})
// 根据日期获取人人车今日新增数据-3
app.post('/renrentodayadd', function (req, res) {
  const { dt } = req.body
  db(`select * from renrenche_ershouche_3 where SUBSTRING(dt,1,10)="${dt}"`, (err, result) => {
    if (err) throw err
    res.send({ status: 0, message: '获取车源信息成功', data: result })
  })
})

app.listen(3000, function () {
  // 设置服务端端口为3000,即：http://127.0.0.1:3000
  // const host = server.address().address

  // const port = server.address().port

  console.log('应用实例，访问地址为 http://%s:%s')
})
